//
//  AccountStorage.swift
//  Exam-iOS
//
//  Created by lishengfeng on 2018/2/7.
//  Copyright © 2018年 李胜锋. All rights reserved.
//

import Foundation
import SwiftKeychainWrapper
import  ObjectMapper


let AccountLevelIdChangedNotification = "AccountLevelIdChangedNotification"

class AccountStorage {
    
    public static var `default`: AccountStorage { return AccountStorage.sharedInstance }
    
    private static let sharedInstance = AccountStorage()
    private(set) var account: AccountModel
    
    init() {
        account = AccountStorage.fetchAccount()
    }
    
    ///更新、保存用户信息
    public func updateAccount(_ account: AccountModel? = nil) {
        if let tempAccount = account {
            self.account.update(tempAccount)
        }
        storageAccount()
    }
    
    ///判断是否登录
    public func isLogin() -> Bool {
        if account.token != nil {
            return true
        }
        return false
    }
    
    //获取本地账号
    private static func fetchAccount() -> AccountModel {
        guard let path = filePath() else { return AccountModel() }
        guard let account = NSKeyedUnarchiver.unarchiveObject(withFile: path) as? AccountModel
            else {
                return AccountModel()
        }
        return account
    }
    
    //保存账号
    @discardableResult //忽略返回值
    private func storageAccount() -> Bool {
    
        //序列化
        guard let path = AccountStorage.filePath() else { return false }
         //将account转换成二进制数据存到文件
        let result = NSKeyedArchiver.archiveRootObject(account, toFile: path)
        return result
    }
    
    //删除账号信息
    func removeAccount() {
        //清空用户信息
        account.clean()
        
        guard let path = AccountStorage.filePath() else { return }
        let fileManager = FileManager.default
        if fileManager.fileExists(atPath: path) {
            do
            {
                try fileManager.removeItem(atPath: path)
            }
            catch let error {
                lsf_alert(error.localizedDescription)
            }
        }
    }
    
    //存储文件路径
    private static func filePath() -> String? {
        guard let direct = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.allDomainsMask, true).first else { return nil }
        let path = direct + "/account.txt"
        return path
    }
}


// MARK:-
// MARK: Keychain存储手机、密码
extension AccountStorage {
    
    // */
    ///手机号
    var mobile:String? {
        get {
            return KeychainWrapper.standard.string(forKey: "mobile")
        }
        
        set {
            if newValue != nil {
                KeychainWrapper.standard.set(newValue!, forKey: "mobile")
            }
            else{
                KeychainWrapper.standard.removeObject(forKey: "mobile")
            }
        }
    }
    
    ///密码
    var password:String? {
        get {
            return KeychainWrapper.standard.string(forKey: "password")
        }
        set {
            if newValue != nil {
                KeychainWrapper.standard.set(newValue!, forKey: "pasword")
            }
            else{
                KeychainWrapper.standard.removeObject(forKey: "pasword")
            }
        }
    }
}
